swift 简单的Time单例,延迟,循环,任你选
import Foundation
typealias ZZWTimeBlock = () -> Void
class ZZWTimeManager: NSObject{
static let sharedInstance = ZZWTimeManager()
var cycyleTimer : Timer?
private var block : ZZWTimeBlock?
// 添加定时器
func addCycleTimer( delay:Double, repeats: Bool) {
if let cycyleTimer = ZZWTimeManager.sharedInstance.cycyleTimer {
cycyleTimer.invalidate()
}
ZZWTimeManager.sharedInstance.cycyleTimer = Timer(timeInterval: delay, target: self, selector: #selector(makeBlePowerCheck), userInfo: nil, repeats: repeats)
RunLoop.main.add(cycyleTimer!, forMode:RunLoopMode.commonModes)
}
// 移除定时器
public func removeCycleTimer() {
// 从运行循环中移除
ZZWTimeManager.sharedInstance.cycyleTimer?.invalidate()
ZZWTimeManager.sharedInstance.cycyleTimer = nil
}
deinit {
removeCycleTimer()
}
@objc private func makeBlePowerCheck() {
block!()
}
func timeOnDoDoDo(timeB: @escaping ZZWTimeBlock) {
block = timeB
}
}
简单使用 :
ZZWTimeManager.sharedInstance.addCycleTimer(60, true)
ZZWTimeManager.sharedInstance.timeOnDoDoDo {
print(“哈哈哈哈哈”)
}